import express, {Response,Request } from 'express';
import smsService from '../services/smsService.js';
const router = express.Router();

/**
 * @swagger
 * /sms/sendCode:
 *   post:
 *     summary: 发送短信验证码
 *     description: 向指定手机号发送短信验证码
 *     tags:
 *       - SMS
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - phone
 *             properties:
 *               phone:
 *                 type: string
 *                 description: 接收验证码的手机号
 *                 default: "17091266225"
 *     responses:
 *       200:
 *         description: 验证码发送成功
 *       400:
 *         description: 手机号格式错误
 *       500:
 *         description: 短信发送失败
 */

router.post('/sendCode', async (req: Request, res: Response) => {
  try {
    const { phone } = req.body;
    // 验证手机号格式
    if (!/^1[3-9]\d{9}$/.test(phone)) {
       res.status(400).json({ code: 400, message: '手机号格式不正确' });
       return;
    }

    await smsService.sendCode(phone);
    res.json({ code: 200, message: '验证码发送成功' });
  } catch (err) {
    console.error('发送短信验证码失败:', err);
    res.status(500).json({ code: 500, message: '验证码发送失败' });
  }
});

export default router;
